package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.queryObject.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.queryObject.StrategyQueryObject;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.StrategyStatisVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategiesController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    /**
     * 攻略前端页面列表 api
     * @return
     */
    @GetMapping("/query")
    @ResponseBody
    public Object query(StrategyQueryObject qo){

        Page<Strategy> query = strategyService.query(qo);

        return JsonResult.success(query);
    }

    /**
     *
     * @return
     */
    @GetMapping("/themes")
    @ResponseBody
    public Object themes(){

        List<StrategyTheme> list = strategyThemeService.list();

        return JsonResult.success(list);
    }

    /**
     * 攻略明细
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Object detail(String id){

        Strategy strategy = strategyService.get(id);

        return JsonResult.success(strategy);
    }

    /**
     * 攻略评论分页
     * @param qo
     * @return
     */
    @GetMapping("/comments")
    @ResponseBody
    public Object comments(StrategyCommentQueryObject qo){

        Page<StrategyComment> query = strategyCommentService.query(qo);

        return JsonResult.success(query);
    }

    /**
     * 攻略评论添加
     * @return
     */
    @PostMapping("/addComment")
    @RequiredLogin // 登录了才可以进行评论
    public Object addComment(StrategyComment strategyComment,@UserParam UserInfo userInfo){
        //使用登录用户参数解析器来传入当前用户对象

        //每进行一次评论 , 评论数就加 1
        strategyStatisVoRedisService.commentIncr(strategyComment);

        strategyCommentService.save(strategyComment,userInfo);

        return JsonResult.success();
    }

    /**
     * 攻略评论点赞
     * @return
     */
    @PostMapping("/commentThumb")
    @RequiredLogin // 登录了之后才可以进行评论点赞
    public Object commentThumb(String cid, @UserParam UserInfo userInfo){

        strategyCommentService.commentThumb(cid,userInfo);

        return JsonResult.success();
    }

    /**
     * 攻略阅读统计数
     * 给阅读数+1,并查出新的阅读数
     * @param sid  对应攻略的id
     * @return
     */
    @GetMapping("/statisVo")
    public Object statisVo(String sid){

        strategyStatisVoRedisService.viewnumIncr(sid);

        StrategyStatisVo strategyStatisVo = strategyStatisVoRedisService.getStrategyStatisVo(sid);

        return JsonResult.success(strategyStatisVo);
    }

    /**
     * 用户收藏攻略操作
     * @param sid
     * @param userInfo
     * @return
     */
    @PostMapping("/favor")
    @RequiredLogin//登录后才能进行收藏
    public Object favor(String sid,@UserParam UserInfo userInfo){

        boolean ret = strategyStatisVoRedisService.addFavor(sid,userInfo.getId());

        return JsonResult.success(ret);
    }

    /**
     * 给攻略点赞
     * @param sid   要点赞的攻略的id
     * @return
     */
    @PostMapping("/strategyThumbup")
    @RequiredLogin
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){

        boolean ret = strategyStatisVoRedisService.strategyThumbup(sid , userInfo.getId());

        return JsonResult.success(ret);
    }

}
